package com.stu.mall.common.valid;

import com.stu.mall.common.exception.BizCodeEnum;
import com.stu.mall.common.exception.RRException;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author admin
 * @Title:
 * @Package
 * @Description:
 * @date 2022/1/25 10:31
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (vals.length == 0) {
            throw new RRException("", BizCodeEnum.VAILD_EXCEPTION.getCode());
        }
        for (int v : vals) {
            set.add(v);
        }
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }


}
